﻿using System;
using System.Collections.Generic;
using System.Linq;

using TrainingsApp.WebModel;
using TrainingsApp.WebModel.Other;
using TrainingsApp.WebModel.Route;
using TrainingsApp.Model;

namespace TrainingsApp.IPADApp.UserControls2 {
    public partial class UC_Menu : AccountControl {
        List<MenuItem> menu = new List<MenuItem>();

        protected void Page_Load(object sender, EventArgs e) {

            if (!this.CurPage.IsLogined) {
                this.Visible = false;
                return;
            }


            string url = this.CurPage.RouteHelper.GetUrl(RouteNames.Index);
            if (CanVisit(url))
                menu.Add(new TrainingsApp.WebModel.Other.MenuItem {
                    Title = "提醒",
                    Class = GetClass(AccountMenuType.Index),
                    Url = url
                });


            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Preview_Index);
            if (CanVisit(url))
                menu.Add(new MenuItem {
                    Title = "前概念",
                    Class = GetClass(AccountMenuType.Preview),
                    Url = url
                });


            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Question_Index);
            menu.Add(new MenuItem {
                Title = "练习",
                Class = GetClass(AccountMenuType.QuestionBank),
                Url = url
            });


            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Dependency_Index);
            menu.Add(new MenuItem {
                Title = "问卷",
                Class = GetClass(AccountMenuType.Dependency),
                Url = url
            });

            url = this.CurPage.RouteHelper.GetUrl(RouteNames.PeiFang_Index);
            menu.Add(new MenuItem {
                Title = "配方",
                Class = GetClass(AccountMenuType.PeiFang),
                Url = url
            });

            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Report_Index);
            menu.Add(new MenuItem {
                Title = "报告",
                Class = GetClass(AccountMenuType.Report),
                Url = url
            });

            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Other_Index);
            menu.Add(new MenuItem {
                Title = "综合",
                Class = GetClass(AccountMenuType.Other),
                Url = url
            });

            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Exchange_Index);
            menu.Add(new MenuItem {
                Title = "交流",
                Class = GetClass(AccountMenuType.Exchange),
                Url = url
            });

            url = this.CurPage.RouteHelper.GetUrl(RouteNames.Exchange_Index);
            menu.Add(new MenuItem {
                Title = "设置",
                Class = GetClass(AccountMenuType.Setting),
                Url = url
            });


            rptDataSource.DataSource = menu;
            rptDataSource.DataBind();
        }


        private bool CanVisit(string url) {
            if (!this.CurPage.IsAuthenticated) return true;

            UserContext user = this.CurPage.CurrentUser;
            if (this.CurPage.IsAuthenticated && (user.FunctionRights == null || user.FunctionRights.Count == 0)) {
                return false;


            } if (!user.FunctionRights.Where(a => !string.Equals(a.LINKURL, null)).ToList().Exists(a => a.LINKURL.ToUpper().Contains(Request.FilePath.ToUpper())) && CurPage.IsAuthenticated) {
                return false;
            }
            return true;

        }


        private string GetClass(AccountMenuType type) {
            return this.CurAccountPage.MenuType == type ? "current" : "";

        }
    }
}